package com.downbox.controller;

import com.downbox.domain.QuotaDaily;
import com.downbox.dto.DrawResp;
import com.downbox.dto.HistoryItemDTO;
import com.downbox.dto.PageResp;
import com.downbox.dto.R;
import com.downbox.repository.QuotaDailyRepository;
import com.downbox.security.UserContext;
import com.downbox.service.ContentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 抽奖控制器
 */
@RestController
@RequestMapping("/draw")
@Tag(name = "抽奖相关接口")
public class DrawController {

    @Autowired
    private ContentService contentService;
    
    @Autowired
    private QuotaDailyRepository quotaDailyRepository;

    @GetMapping("/content")
    @Operation(summary = "抽奖获取内容")
    public R<DrawResp> drawContent(@RequestParam(required = false) String type) {
        Long userId = UserContext.getUserId();
        DrawResp resp = contentService.drawContent(userId, type);
        return R.success(resp);
    }

    @GetMapping("/has-drawn")
    @Operation(summary = "检查今日是否已抽奖")
    public R<Boolean> hasDrawnToday() {
        Long userId = UserContext.getUserId();
        boolean hasDrawn = contentService.hasDrawnToday(userId);
        return R.success(hasDrawn);
    }

    @GetMapping("/bonus/receive")
    @Operation(summary = "领取奖励次数")
    public R<Void> receiveBonus() {
        Long userId = UserContext.getUserId();
        contentService.receiveBonus(userId);
        return R.success();
    }
    
    @GetMapping("/status")
    @Operation(summary = "获取抽奖状态")
    public R<Map<String, Object>> getDrawStatus() {
        Long userId = UserContext.getUserId();
        LocalDate today = LocalDate.now();
        
        // 获取用户今日配额
        Optional<QuotaDaily> quotaOptional = quotaDailyRepository.findByUidAndDate(userId, today);
        QuotaDaily quota = quotaOptional.orElseGet(() -> {
            QuotaDaily newQuota = new QuotaDaily();
            newQuota.setUid(userId);
            newQuota.setDate(today);
            newQuota.setDrawUsed(false);
            newQuota.setBonusLeft(0);
            return newQuota;
        });
        
        // 构建响应数据
        Map<String, Object> data = new HashMap<>();
        data.put("canDraw", !quota.getDrawUsed() || quota.getBonusLeft() > 0);
        data.put("remainingDraws", !quota.getDrawUsed() ? 1 : 0);
        data.put("todayDraws", quota.getDrawUsed() ? 1 : 0);
        data.put("bonusDraws", quota.getBonusLeft());
        
        return R.success(data);
    }
    
    @PostMapping("/perform")
    @Operation(summary = "执行抽奖")
    public R<DrawResp> performDraw(@RequestBody Map<String, List<String>> request) {
        Long userId = UserContext.getUserId();
        List<String> moods = request.get("moods");
        // 暂时忽略moods参数，直接调用抽奖服务
        DrawResp resp = contentService.drawContent(userId, null);
        return R.success(resp);
    }
    
    @PostMapping("/bonus")
    @Operation(summary = "执行额外抽奖")
    public R<DrawResp> performBonusDraw() {
        Long userId = UserContext.getUserId();
        // 额外抽奖与普通抽奖使用相同的服务方法
        DrawResp resp = contentService.drawContent(userId, null);
        return R.success(resp);
    }
    
    @GetMapping("/result/{historyId}")
    @Operation(summary = "获取抽奖结果详情")
    public R<Map<String, Object>> getDrawResultDetail(@PathVariable Long historyId) {
        // 暂时返回模拟数据
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> content = new HashMap<>();
        content.put("title", "礼物详情");
        content.put("content", "一份来自远方的温暖祝福");
        content.put("tags", List.of("#祝福"));
        content.put("source", "系统");
        result.put("type", "text");
        result.put("content", content);
        return R.success(result);
    }
}
